<?php

namespace App\Http\Controllers\Api;


use App\Http\Queries\TopicQuery;
use App\Http\Requests\Api\StoreTopicRequest;
use App\Http\Requests\Api\UpdateTopicRequest;
use App\Http\Resources\TopicResource;
use App\Models\Topic;
use App\Models\User;
use Illuminate\Http\Request;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

class TopicsController extends Controller {
    public function index(Request $request, TopicQuery $topicQuery) {
        return $this->errorResponse(403,'你还未被认证',1001);
        $topics = $topicQuery->paginate();
        return TopicResource::collection($topics);
    }

    public function store(StoreTopicRequest $request, Topic $topic) {
        $topic->fill($request->all());
        $topic->user_id = $request->user()->id;
        $topic->save();

        return new TopicResource($topic);
    }

    public function update(UpdateTopicRequest $request, Topic $topic) {
        $this->authorize('update', $topic);
        $topic->update($request->all());

        return new TopicResource($topic);
    }

    public function destroy(Topic $topic) {
        $this->authorize('destroy', $topic);
        $topic->delete();
        return response(null, 204);
    }

    public function show(Request $request, $topicId,TopicQuery $topicQuery) {
        $topic = $topicQuery->findOrFail($topicId);
        return new TopicResource($topic);
    }

    //某人发的帖子
    public function userIndex(Request $request,User $user,TopicQuery $topicQuery) {
        $topics = $topicQuery->where('user_id',$user->id)->paginate();
        return TopicResource::collection($topics);
    }

}
